#include "clientTask.h"

int dirStackInit(dirStack_t *pdirStack){
    //pdirStack = 
    memcpy(pdirStack->dir, "/", -1);
    pdirStack->top = -1;
    return 0;
}
int getDirStackTop(dirStack_t *pdirStack, char dir[]){
    if(pdirStack->top == -1){
        printf("dirStack is empty!\n");
        return -1;
    }
    else{
        memcpy(dir, pdirStack->dir, strlen(pdirStack->dir));
    }
}
int pushStack(dirStack_t *pdirStack, const char* dirStr){
    //入栈时栈是否满
    if(pdirStack->top >= MAX_SIZE-1){
        printf("dirStack is full!\n");
        return -1;
    }
    (pdirStack->top)++;
    memcpy(pdirStack->dir, dirStr, strlen(dirStr));
    return 0;
}
int popStack(dirStack_t *pdirStack){
    //出栈时栈是否空, ..=出栈, .=什么都不做
    if(pdirStack->top >= 0){
        --pdirStack->top;
    }
    return 0;
}
int achieveDir(dirStack_t *pdirStack, char path[]){
    //遍历目录栈
    if(-1 == pdirStack->top){
        //目录为空，则返回.和..
        memcpy(path, pdirStack->dir, strlen(pdirStack->dir));
        printf("achieveDir path = %s\n", path);
    }
    else{
        for(int i = pdirStack->top; i >= 0; --i){
            sprintf(path, "%s%s%s", path, pdirStack[i].dir, " ");
            printf("path = %s\n", path);
        }
    }
    return 0;
}

int dirRepalce(dirStack_t *pdirStack, char* cmdStrArr[], int cmdNum, char dir[]){
    for(int i = 0; i < cmdNum; ++i){
        if(strcmp(cmdStrArr[i], ".") == 0){
            continue;
        }
        else if(strcmp(cmdStrArr[i], "..") == 0){
            popStack(pdirStack);
        }
        else{
            pushStack(pdirStack, cmdStrArr[i]);
        }
    }
    achieveDir(pdirStack, dir);
    return 0;
}